﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Utilities.QuickSql;
using MySql.Data;

namespace Deviseur_Cs2i_Car
{
    public partial class AddCouleurCaross : Form
    {
       private COULEURCAROSS CouleurCaross;
       string UrlImage;
        public AddCouleurCaross()
        {
            InitializeComponent();
            this.Text = "Ajouter un CouleurCaross de voiture";
            this.buttonAddCouleurCaross.Text = "Ajouter";
            this.checkBox1.Checked = true;
            Console.WriteLine(this.checkBox1.Checked.ToString());
        }
        public AddCouleurCaross(int NumCouleurCaross)
        {
            InitializeComponent();
            this.Text = "Modifier un CouleurCaross de voiture";
            this.buttonAddCouleurCaross.Text = "Modifier";
            string check;
            Table CouleurCarossVoiture = Program.Db.QueryTable("SELECT LibelleCouleurCaross, MetalCouleurCaross, PrixCouleurCaross FROM COULEURCAROSS WHERE NumCouleurCaross = " + NumCouleurCaross);
            foreach (Record r in CouleurCarossVoiture)
            {
                this.textLibelle.Text = r.Values[0].ToString();
                this.checkBox1.Text = r.Values[1].ToString();
                this.textPrix.Text = r.Values[2].ToString();
                if (this.checkBox1.Checked == true)
                {
                    check = "O";
                }
                else
                {
                    check = "N";
                }
                CouleurCaross = new COULEURCAROSS(this.textLibelle.Text.ToString(), check, Convert.ToDouble(textPrix.Text.ToString()));
            }
        }

        private void buttonSubmitCouleurCaross_Click(object sender, EventArgs e)
        {
            string check;
            if (this.buttonAddCouleurCaross.Text == "Ajouter")
            {
                if (this.checkBox1.Checked == true)
                {
                    check = "O";
                }
                else
                {
                    check = "N";
                }

                CouleurCaross = new COULEURCAROSS(this.textLibelle.Text.ToString(), check, Convert.ToDouble(textPrix.Text.ToString()));
                
                Thread addThread = new Thread(new ThreadStart(addCouleurCaross_Thread));
                addThread.Start();
            }
            else if (this.buttonAddCouleurCaross.Text == "Modifier")
            {
                if (this.checkBox1.Checked == true)
                {
                    check = "O";
                }
                else
                {
                    check = "N";
                }
                Console.WriteLine(check);
                CouleurCaross.set_LibelleCouleurCaross(this.textLibelle.Text.ToString());
                CouleurCaross.set_MetalCouleurCaross(check);
                Thread updateThread = new Thread(new ThreadStart(updateCouleurCaross_Thread));
                updateThread.Start();
            }
            
        }

        private void addCouleurCaross_Thread()
        {
            Invoke(new MethodInvoker(Disabled));
            CouleurCaross.new_CouleurCaross();
            Invoke(new MethodInvoker(CloseFen));
        }
        private void updateCouleurCaross_Thread()
        {
            Invoke(new MethodInvoker(Disabled));
            CouleurCaross.modify_CouleurCaross();
            Invoke(new MethodInvoker(CloseFen));
            // Coucou
        }

        private void Disabled()
        {
            this.textLibelle.Enabled = false;
            this.textPrix.Enabled = false;
            this.checkBox1.Enabled = false;
            this.buttonAddCouleurCaross.Enabled = false;
        }
        private void CloseFen()
        {
            this.Close();
        }

        private void buttonAddCouleurCaross_Click(object sender, EventArgs e)
        {
            if (this.buttonAddCouleurCaross.Text == "Ajouter")
            {
                string check;
                if (this.checkBox1.Checked == true)
                {
                    check = "O";
                }
                else
                {
                    check = "N";
                }
                CouleurCaross = new COULEURCAROSS(this.textLibelle.Text.ToString(), check, Convert.ToDouble(textPrix.Text.ToString()));
                CouleurCaross.set_Image(this.openFileDialog.FileName.ToString());
                Thread addThread = new Thread(new ThreadStart(addCouleurCaross_Thread));
                addThread.Start();
            }
            else if (this.buttonAddCouleurCaross.Text == "Modifier")
            {
                CouleurCaross.set_LibelleCouleurCaross(this.textLibelle.Text.ToString());
                CouleurCaross.set_MetalCouleurCaross(this.checkBox1.Text.ToString());
                Thread updateThread = new Thread(new ThreadStart(updateCouleurCaross_Thread));
                updateThread.Start();
            }
        }

        private void buttonParcourir_Click(object sender, EventArgs e)
        {
            this.openFileDialog.ShowDialog();
        }

        private void openFileDialog_FileOk(object sender, CancelEventArgs e)
        {
            this.UrlImage = this.openFileDialog.FileName.ToString();
            pictureBox1.Image = Image.FromStream(System.IO.File.OpenRead(this.openFileDialog.FileName.ToString()));
        }
    }
}
